﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class ChiTietPhieuTraHangDAO
    {
        public string ThemCTPhieuTraHang(ChiTietPhieuTraHangDTO ctpthDTO)
        {
            string mess = "";

            string sql = "Insert into";
            sql += " CHITIETPHIEUTRAHANG(MaChiTietPhieuTra,MaPhieuTra, MaMatHang, MaXuatXu, MaDonViTinh, MaLoiMatHang, SoLuong, DonGia)";
            sql += " values(@MaChiTietPhieuNhap, @MaPhieuNhap, @MaMatHang, @MaXuatXu, @MaDonViTinh, @MaLoiMatHang, @SoLuong, @DonGia)";
            SqlConnection conn = DataProvider.ConnectDB();
            try
            {
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlParameter para;
                para = cmd.Parameters.Add("@MaChiTietPhieuNhap", SqlDbType.Int);
                para.Value = ctpthDTO.MaCTPhieuTra;
                para = cmd.Parameters.Add("@MaPhieuNhap", SqlDbType.Int);
                para.Value = ctpthDTO.MaPhieuTra;
                para = cmd.Parameters.Add("@MaMatHang", SqlDbType.Int);
                para.Value = ctpthDTO.MaMatHang;
                para = cmd.Parameters.Add("@MaXuatXu", SqlDbType.Int);
                para.Value = ctpthDTO.MaXuatXu;
                para = cmd.Parameters.Add("@MaDonViTinh", SqlDbType.Int);
                para.Value = ctpthDTO.MaDonViTinh;
                para = cmd.Parameters.Add("@MaLoiMatHang", SqlDbType.Int);
                para.Value = ctpthDTO.MaLoiMatHang;
                para = cmd.Parameters.Add("@SoLuong", SqlDbType.Int);
                para.Value = ctpthDTO.SoLuong;
                para = cmd.Parameters.Add("@DonGia", SqlDbType.Float);
                para.Value = ctpthDTO.GiaNhap;

                cmd.ExecuteNonQuery();
                conn.Close();
            }
            catch (SqlException ex)
            {
                mess = ex.Message;
            }
            finally
            {
                conn.Close();
            }
            return mess;
        }

        public List<ChiTietPhieuTraHangDTO> DanhSachChiTietPTH()
        {
            List<ChiTietPhieuTraHangDTO> listDS = new List<ChiTietPhieuTraHangDTO>();
            string sql = "Select * From CHITIETPHIEUTRAHANG";

            SqlConnection conn = DataProvider.ConnectDB();
            SqlCommand cmd = new SqlCommand(sql, conn);
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                ChiTietPhieuTraHangDTO ctpthDTO = new ChiTietPhieuTraHangDTO();
                ctpthDTO.MaCTPhieuTra = reader.GetInt32(0);
                ctpthDTO.MaPhieuTra = reader.GetInt32(1);
                ctpthDTO.MaMatHang = reader.GetInt32(2);
                ctpthDTO.MaXuatXu = reader.GetInt32(3);
                ctpthDTO.MaDonViTinh = reader.GetInt32(4);
                ctpthDTO.MaLoiMatHang = reader.GetInt32(5);
                ctpthDTO.SoLuong = reader.GetInt32(6);
                ctpthDTO.GiaNhap = reader.GetDouble(7);
                listDS.Add(ctpthDTO);
            }
            conn.Close();
            return listDS;
        }

        public int LayMaChiTietPTH()
        {
            int max = 0;
            List<ChiTietPhieuTraHangDTO> listCTPTH = DanhSachChiTietPTH();
            if (listCTPTH.Count < 1)
                return max;
            for (int i = 0; i < listCTPTH.Count; i++)
            {
                if (listCTPTH[i].MaCTPhieuTra > max)
                    max = listCTPTH[i].MaCTPhieuTra;
            }
            return max;
        }
    }
}

